/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.message;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.common.util.TimeUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.account.SysUser;
import com.unis.model.message.Message;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2013-02-24
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Message
     ****************************************/
    public void insert(Message message) throws Exception {
        message.setXxid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(message);
    }

    /****************************************
     * Update a instace of Message
     ****************************************/
    public void update(Message message) throws Exception {
        if ("1".equals(message.getXxzt())) {
            message.setXxydsj(TimeUtil.getNow());
        }
        iDataSet4iBatis.update(message);
    }

    /****************************************
     * Delete a instace of Message
     ****************************************/
    public void delete(Message message) throws Exception {
        iDataSet4iBatis.delete(message);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Message message) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Message message) throws Exception {
        String statementId = message.getStatmentId();
        String mainId = message.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> messageList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Message model = new Message();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            messageList.add(model);
        }
        iDataSet4iBatis.batchDelete(messageList);
    }
    
    /****************************************
     * Update instaces of SysUser
     ****************************************/
    public void batchUpdate(Message message) throws Exception {
        String statementId = message.getStatmentId();
        String mainId = message.getId();
        String[] ids = mainId.split(",");
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser) currentUser.getPrincipal();
        for (String id : ids) {
            Message model = new Message();
            model.setStatmentId(statementId);
            model.setXxid(id.trim());
            model.setScbs("1");           
            model.setScr(user.getUsername());
            model.setScrid(user.getUserId());
            iDataSet4iBatis.update(model);
        }
    }

    /****************************************
     * Query instaces of Message
     ****************************************/
    public List<BaseModel> queryAsList(Message message) throws Exception {
        String queryListStatement = "";
        // 只查询当前用户的消息
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser) currentUser.getPrincipal();
        message.setXxjsrid(user.getUserId());
        message.setXxjsrxm(user.getUsername());
        if (message.isPaging()) {
            queryListStatement = message.getStatmentId();
            message.setStatmentId("Message.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(message);
            message.setTotalCount(baseModel.getTotalCount());
            message.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(message);
    }

    /****************************************
     * Query a instaces of Message
     ****************************************/
    public BaseModel queryAsObject(Message message) throws Exception {
        return iDataSet4iBatis.queryAsObject(message);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }

}
